#include<iostream>
#include<signal.h>
#include"oj_control.hpp"
#include"../Com/httplib.h"

using namespace httplib;
using namespace ns_control;

Control *ctl_ptr = nullptr;

void Recovery(int sig)
{
    ctl_ptr->Recovery();
    LOG(INFO) << "所有主机上线..." << std::endl;
}

int main()
{
    Server svr;
    Control ctl;
    ctl_ptr = &ctl;

    signal(SIGQUIT,Recovery);

    //获取所有的题目列表
    svr.Get("/acq_all_questions",[&ctl](const Request& req, Response& resp){
        std::string html;
        ctl.AllQuestions(&html);
        resp.set_content(html,"text/html;charset=utf-8");
        // resp.set_content("这是所有的题目列表", "text/plain;charset=utf-8");
    });

    //根据题目编号获取题目内容
    svr.Get(R"(/acq_question/(\d+))",[&ctl](const Request& req, Response& resp){
        std::string number = req.matches[1];
        std::string html;
        ctl.Question(number,&html);
        resp.set_content(html,"text/html;charset=utf-8");
        // resp.set_content("这是题目：" + number,"text/plain;charset=utf-8");
    });

    //用户提交代码使用判题功能
    svr.Post(R"(/judge/(\d+))",[&ctl](const Request& req, Response& resp){
        std::string number = req.matches[1];
        std::string result;
        ctl.Judge(number,req.body,&result);
        resp.set_content(result,"application/json;charset=utf-8");
        // resp.set_content("正在判题: " + number,"text/plain;charset=utf-8");
    });
    
    svr.set_base_dir("./wwwroot");
    svr.listen("0.0.0.0",8080);
    return 0;
}